<template>
	<view class="bj" id="bj">
		<view class="item_box" v-if="priceList.length > 0">
			
			<view class="master"  v-for="item in priceList" :key="item.phone">
				<view class="masterInfo">
					<view class="avatar">
						<view class="icon">
							<text class="iconfont icon-yonghu"></text>
						</view>
						<view class="masterName">
							{{ item.realname }}
						</view>
						
					</view>
					<view class="phone" >
						<view class="addtime">
							{{ item.addtime }}
						</view>
						<text class="iconfont icon-shouji"></text>
					</view>
				</view>
				<view class="content">
					<view class="price tag">
						<van-tag mark type="primary">报价</van-tag> <text>{{ item.orderprice }}</text>
					</view>
					<view class="imprest tag">
						<van-tag mark type="success">定金</van-tag> <text>{{ item.imprest }}</text>
					</view>
				</view>
				<view class="btnbox">
					<van-button icon="success" type="info" size="small" @click="getSetting(item)">接受报价</van-button>
				</view>
			</view>
		</view>
		<view class="none" v-else>
			暂无报价
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderid: "",
				priceList: [
					
				],
				access_token: "",
			}
		},
		onLoad( e ) {
			this.orderid = e.orderid
		},
		onShow() {
			this.getPriceList()
			this.getAccess_token()
			this.reflash_token()
		},
		computed: {
			token() {
				return this.$store.state.token
			},
		},
		methods: {
			// 获取订单报价列表
			async getPriceList() {
				//console.log(this.orderid);
				let res = await this.$Request("POST","/order/offerpricelist",{ orderid: this.orderid })
				// console.log(res);
				let code = res.data.code
				if(code == 200 || code == 202 || code == 204) {
					this.priceList = res.data.data
				} else {
					this.priceList = []
				}
				
			},
			// 拨号
			callPhone( phone ) {
				console.log(phone);
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			// 确认师傅
			async ConfirmMaster( item ) {
				// console.log(item);
				let orderid = item.orderid
				let id = item.id
				 let res = await this.$Request("POST","/order/shifu", { token: this.token, orderid, id })
				 // console.log(res);
				 if(res.data.code == 202) {
					 this.$store.commit("getConfirmMaster", true)
					 uni.showToast({
					 	title: "确认成功",
						icon: "success"
					 })
					 // this.sendMsg({
						//  address: item.address,
						//  name: item.name,
						//  servicetime: item.servicetime,
						//  state: "待支付",
						//  userphone: item.userphone,
						//  openid: item.openid,
					 // })
					 setTimeout(() => {
						 uni.switchTab({
						 	url:"/pages/order/order"
						 }, 1000)
					 })
				 }
			},
			// 获取订阅消息授权
			getSetting( item ) {
				console.log(item);
				let self = this
				uni.getSetting({
					withSubscriptions: true,
					success( res ) {
						let isauth = res.authSetting["scope.subscribeMessage"]
						
										// 用户同意授权
										wx.requestSubscribeMessage({
										  tmplIds: [
											  '-fT7TqL9Jza4TUhdfMuh6AC39GxWZj76i2bEpXJsxNk',
											  'fxviPJF4R7JGFzpRR8d2JiZJu7Sucm7_kYzXOGDlv_w',
											  '-EmYCJVoB59CJtCx892Q6HF5D-K9PeTQaWgKDvPS4D4'
											  ],
										  success (res) {
											  // console.log(res);
											  let msg = res.errMsg == "requestSubscribeMessage:ok"
											  if(msg) {
												    self.ConfirmMaster(item)
											  } else {
												  return false 
											  }
										  },
										  fail(err) {
											  console.log(err);
										  }
										})
						},
				})
				
			},
			// 下单成功后 发送消息
			sendMsg( data ) {
				let self = this
				
				uni.request({
					url: `https://www.awoojing.club/admin/sendMsg`,
					method: "POST",
					data: {
						touser: `${data.openid}`,
						access_token: `${self.access_token}`,
						page: '/pages/index/index',
						lang: 'zh_CN',
						data: {
						  thing1: {
							value: `${data.address}`
						  },
						  thing4: {
							value: `${data.name}`,
						  },
						  date5: {
							value: `${data.servicetime}`
						  },
						  phrase6: {
							value: `${data.state}`
						  },
						  phone_number8: {
							  value: `${data.userphone}`
						  }
						  
						},
						templateId: '-fT7TqL9Jza4TUhdfMuh6AC39GxWZj76i2bEpXJsxNk',
						
					},
					success( res ) {
						console.log(res);
					},
					fail( err ) {
						console.log(err);
					}
				})
				
					
			},
			//  进入当前页面后获取access_token
			getAccess_token() {
				let self = this
				uni.request({
					url: "https://www.awoojing.club/admin/getAccess_token",
					method:"POST",
					data: {},
					success(res) {
						// console.log(res);
						if(res.data.code == 200) {
							self.access_token = res.data.data
						}
					}
				})
				
			},
			// 定时刷新access_token
			reflash_token() {
				let self = this 
				setInterval(() => {
					self.getAccess_token()
				}, 1000 * 60 * 10)
			},
			
		}
	}
</script>

<style scoped lang="scss">
	#bj {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		.none {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #aaa;
		}
		.item_box {
			
			.master {
				width: 100%;
				box-sizing: border-box;
				padding: 10rpx;
				border-radius: 10rpx;
				box-shadow: 0 0 5rpx #007eff;
				margin: 20rpx auto;
				.masterInfo {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					height: 70rpx;
					line-height: 70rpx;
					border-bottom: 1px solid #f5f5f5;
					color: #007eff;
					.avatar {
						display: flex;
						align-items: flex-end;
						.masterName {
							margin-left: 15rpx;
						}
						
					}
					.phone {
						display: flex;
						align-items: center;
						.addtime {
							margin-left: 15rpx;
							font-size: 24rpx;
							margin-right: 15rpx;
						}
					}
				}
				.content {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					padding: 30rpx 0;
					.tag {
						width: 250rpx;
						display: flex;
						align-items: center;
						text-align: center;
						justify-content: center;
						font-size: 30rpx;
						text {
							margin-left: 20rpx;
						}
					}
				}
				.btnbox {
					width: 100%;
					text-align: right;
				}
			}
		}
	}
</style>
